package com.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.constant.WareConstant;
import com.common.utils.PageUtils;
import com.common.utils.Query;
import com.gulimall.ware.dao.PurchaseDao;
import com.gulimall.ware.entity.PurchaseDetailEntity;
import com.gulimall.ware.entity.PurchaseEntity;
import com.gulimall.ware.service.PurchaseDetailService;
import com.gulimall.ware.service.PurchaseService;
import com.gulimall.ware.service.WareSkuService;
import com.gulimall.ware.vo.MergeVo;
import com.gulimall.ware.vo.PurchaseDoneVo;
import com.gulimall.ware.vo.PurchaseItem;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {
    @Autowired
    PurchaseDetailService purchaseDetailService;
    @Autowired
    WareSkuService wareSkuService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getUnreciver(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status", WareConstant.PurchaseStatusEnum.Create.getCode()).or().eq("status",WareConstant.PurchaseStatusEnum.ASSIGNED.getCode()));
        PageUtils pageUtils = new PageUtils(page);
        return pageUtils;
    }
    @Transactional
    @Override
    public void mergeRequired(MergeVo vo) {
        Long purchaseId = vo.getPurchaseId();
        if(StringUtils.isEmpty(purchaseId)){
            PurchaseEntity purchaseEntity=new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.Create.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId=purchaseEntity.getId();
        }

        List<Long> idList = vo.getItems();
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> purchaseDetailEntityList = idList.stream().map(id -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(id);
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return purchaseDetailEntity;
        }).filter(item->{
            return item.getStatus()==WareConstant.PurchaseDetailStatusEnum.CREATED.getCode()||item.getStatus()==WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode();
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(purchaseDetailEntityList);

        PurchaseEntity purchaseEntity=new PurchaseEntity();
        purchaseEntity.setUpdateTime(new Date());
        purchaseEntity.setId(purchaseId);
//        purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.ASSIGNED.getCode());     分配是另外一个方法了
        this.updateById(purchaseEntity);
    }
    @Transactional
    @Override
    public void received(List<Long> idList) {
        List<PurchaseEntity> purchaseEntityList = idList.stream().map(id -> {
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            PurchaseEntity entity = this.getById(id);
            BeanUtils.copyProperties(entity, purchaseEntity);
//            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            purchaseEntity.setUpdateTime(new Date());
            return purchaseEntity;
        }).filter(item -> {
            return item.getStatus() == WareConstant.PurchaseStatusEnum.Create.getCode() || item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode();
        }).map(item->{
            item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            return item;
        }).collect(Collectors.toList());

        this.updateBatchById(purchaseEntityList);


        purchaseEntityList.forEach(purchaseEntity -> {
            Long id = purchaseEntity.getId();
            List<PurchaseDetailEntity> list=purchaseDetailService.getListByPid(id);
            List<PurchaseDetailEntity> collect = list.stream().map(ll -> {
                ll.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                return ll;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(collect);
        });

    }

    @Transactional
    @Override
    public void done(PurchaseDoneVo vo) {
        List<PurchaseItem> purchaseItemList = vo.getItems();
        boolean flag=true;
        List<PurchaseDetailEntity> list=new ArrayList<>();
        for (PurchaseItem purchaseItem : purchaseItemList) {
            PurchaseDetailEntity purchaseDetailEntity=new PurchaseDetailEntity();
            if (purchaseItem.getStatus().equals(WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode())) {
                flag = false;
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode());
            } else {
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
            }
            purchaseDetailEntity.setId(purchaseItem.getItemId());
            list.add(purchaseDetailEntity);

            //更新库存
            PurchaseDetailEntity detailEntity = purchaseDetailService.getById(purchaseItem.getItemId());
            wareSkuService.Order(detailEntity.getSkuId(),detailEntity.getWareId(),detailEntity.getSkuNum());
        }
        //更新采购需求
        purchaseDetailService.updateBatchById(list);

        PurchaseEntity purchaseEntity=new PurchaseEntity();
        if(flag){
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.FINISH.getCode());
        }else{
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        }
        purchaseEntity.setId(vo.getId());
        purchaseEntity.setUpdateTime(new Date());
        //更新采购单
        this.updateById(purchaseEntity);
    }

}